package net.zhengxinyun.performance.controller.News;

import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.api.service.SysFileService;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.NewsEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.NewsFilter;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.news.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2018/12/7.
 */
@RestController
@RequestMapping("/News")
public class NewsController {

    private NewsService newsService;
    private SysFileService sysFileService;
    private TokenService tokenService;

    @Autowired
    public NewsController(NewsService newsService, SysFileService sysFileService, TokenService tokenService) {
        this.newsService = newsService;
        this.sysFileService = sysFileService;
        this.tokenService = tokenService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody NewsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = newsService.insert(param.newsEntity);
            if (!"添加成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody NewsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = newsService.delete(param.newsEntity);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody NewsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = newsService.update(param.newsEntity);
            if (!"更新成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody NewsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            List<NewsFilter> list = newsService.query(param.newsFilter);
            if (list.size() == 0){
                param.newsFilter.setPageNumber(param.newsFilter.getPageNumber()-1);
                list = newsService.query(param.newsFilter);
            }
            Double pageTotal = Util.intGetTotalPage(newsService.queryForCount(param.newsFilter),param.newsFilter.getPageSize());
            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/watchNews")
    public Result<HashMap<String,Object>> watchNews(@RequestBody NewsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            ArrayList<Map<String,Object>> list = newsService.watchNews(param.newsFilter);
            Double pageTotal = Util.intGetTotalPage(newsService.queryForWatchCount(),param.newsFilter.getPageSize());
            NewsEntity newsEntity = newsService.queryTop();
            map.put("top",newsEntity);
            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/setTop")
    public Result<String> updateTop(@RequestBody NewsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = newsService.updateTop(param.newsEntity);
            if (!"置顶成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }
}

class NewsParam{
    public TokenEntity tokenEntity;
    public NewsEntity newsEntity;
    public NewsFilter newsFilter;
}